package com.osi.mes.qm.services;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRow;
import com.datasweep.compatibility.client.ATRowFilter;
import com.datasweep.compatibility.client.DatasweepException;
import com.datasweep.compatibility.client.DependentATRow;
import com.datasweep.compatibility.client.Equipment;
import com.datasweep.compatibility.client.ParentATRow;
import com.datasweep.compatibility.client.ProductionLine;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.client.WorkCenter;
import com.datasweep.compatibility.ui.Time;
import com.osi.mes.base.BaseService;
import com.osi.mes.qm.base.constants.IATGoodChk;
import com.osi.mes.utils.StringUtils;

/**
 * 设备易松脱品检查 带入车间物品检查 玻璃及易碎品检查
 * 
 * @author liting
 */
public class QmGoodChkService extends BaseService implements IATGoodChk
{

	public QmGoodChkService(Context context)
	{
		super(context);
	}

	// delete 设备易松脱品检查项
	public boolean deleteGoodChkItem(ATRow chkItem)
		throws DatasweepException
	{
		boolean isSucc = true;
		String chkItemName = (String) chkItem.getValue(AT_GOOD_CHK_ITEM_NAME);

		String atDefName = AT_EASY_LOOSER_GOOD_CHK_RES;
		ATHandler atHandler = getFunctions().createATHandler(
			atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(
			atDefName);
		rowFilter.forColumnNameEqualTo(
			AT_EL_CHK_RES_CHK_ITEM, chkItemName);

		Vector<ATRow> chkResults = atHandler.getATRowsByFilter(
			rowFilter, false);
		if (chkResults != null)
		{
			for (int i = 0; i < chkResults.size(); i++)
			{
				ATRow chkRes = chkResults.get(i);
				isSucc = super.deleteATRow(chkRes);
				if (!isSucc)
					return isSucc;
			}
		}

		isSucc = super.deleteATRow(chkItem);
		return isSucc;
	}

	// 根据类型查看是否存在对应的检验项
	public ATRow getChkItemByName(String chkItemName, int chkType)
		throws DatasweepException
	{
		Hashtable<String, Object> hashTb = new Hashtable<String, Object>();
		hashTb.put(
			AT_GOOD_CHK_ITEM_NAME, chkItemName);
		hashTb.put(
			AT_GOOD_CHK_TYPE, chkType);

		Vector<ATRow> atRows = super.getATRowData(
			AT_GOOD_CHK_ITEM, hashTb);
		if (atRows == null || atRows.size() == 0)
			return null;

		return atRows.get(0);
	}

	// 保存易脱品的检验结果
	public boolean saveElGoodChkRes(Hashtable hashTb, String routeName, String routeStepName,
		String equName)
		throws Exception
	{
		for (int i = 0; i < hashTb.size(); i++)
		{
			Object resObj = hashTb.get(i);
			if (resObj instanceof Object[])
			{
				Object[] chkResVec = (Object[]) resObj;
				String chkItemName = (String) chkResVec[0];
				int chkRes = new Integer(chkResVec[1].toString()).intValue();

				Hashtable<String, Object> atRowValus = new Hashtable<String, Object>();

				atRowValus.put(
					AT_EL_CHK_RES_ROUTE, routeName);
				atRowValus.put(
					AT_EL_CHK_RES_ROUTE_STEP, routeStepName);
				atRowValus.put(
					AT_EL_CHK_RES_EQU_NAME, equName);
				atRowValus.put(
					AT_EL_CHK_RES_CHK_ITEM, chkItemName);
				atRowValus.put(
					AT_EL_CHK_RESULT, chkRes);
				atRowValus.put(
					AT_EL_CHK_TIME, getFunctions().createTime());
				atRowValus.put(
					AT_EL_CHK_RES_CHK_USER, getFunctions().getCurrentUser().getName());
				if (chkResVec[2] != null)
					atRowValus.put(
						AT_EL_CHK_RES_CORRECTIVE_ACTION, chkResVec[2]);
				if (chkResVec[3] != null)
					atRowValus.put(
						AT_EL_CHK_RES_MARK, chkResVec[3]);

				boolean isSucc = super.addATRow(
					AT_EASY_LOOSER_GOOD_CHK_RES, atRowValus);
				if (!isSucc)
					return false;
			}
		}

		return true;
	}

	// 获得设备的检查结果
	public Vector<ATRow> searchChkResult(ProductionLine pl, WorkCenter wc, Equipment equ,
		Time chkTimeFrom, Time chkTimeTo)
		throws DatasweepException
	{
		Vector atRows = new Vector();
		ATHandler atHandler = getFunctions().createATHandler(
			AT_EASY_LOOSER_GOOD_CHK_RES);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(
			AT_EASY_LOOSER_GOOD_CHK_RES);
		if (pl != null)
			rowFilter.forColumnNameEqualTo(
				AT_EL_CHK_RES_ROUTE, pl.getName());

		if (wc != null)
			rowFilter.forColumnNameEqualTo(
				AT_EL_CHK_RES_ROUTE_STEP, wc.getName());

		if (equ != null)
			rowFilter.forColumnNameEqualTo(
				AT_EL_CHK_RES_EQU_NAME, equ.getName());

		if (chkTimeFrom != null)
		{
			chkTimeFrom = super.getDayStart(chkTimeFrom);
			rowFilter.forColumnNameGreaterThanOrEqualTo(
				AT_EL_CHK_TIME, chkTimeFrom);
		}
		if (chkTimeTo != null)
		{
			chkTimeTo = super.getDayEnd(chkTimeTo);
			rowFilter.forColumnNameLessThan(
				AT_EL_CHK_TIME, chkTimeTo);
		}

		atRows = atHandler.getATRowsByFilter(
			rowFilter, false);
		return atRows;

	}

	// 保存带入车间的检验结果
	public boolean saveBWPGoodChkRes(Hashtable hashTb, String userName)
		throws Exception
	{
		for (int i = 0; i < hashTb.size(); i++)
		{
			Object resObj = hashTb.get(i);
			if (resObj instanceof Object[])
			{
				Object[] chkResVec = (Object[]) resObj;
				String chkItemName = (String) chkResVec[0];
				int inChkRes = new Integer(chkResVec[1].toString()).intValue();
				int outChkRes = new Integer(chkResVec[2].toString()).intValue();

				Hashtable<String, Object> atRowValus = new Hashtable<String, Object>();

				atRowValus.put(
					AT_BWP_USER_NAME, userName);
				atRowValus.put(
					AT_BWP_CHK_ITEM_NAME, chkItemName);
				atRowValus.put(
					AT_BWP_CHK_IN_RES, inChkRes);
				atRowValus.put(
					AT_BWP_CHK_OUT_RES, outChkRes);
				atRowValus.put(
					AT_BWP_CHK_TIME, getFunctions().createTime());
				atRowValus.put(
					AT_BWP_CHK_USER, getFunctions().getCurrentUser().getName());
				if (chkResVec[2] != null)
					atRowValus.put(
						AT_BWP_CHK_CORRECT_ACTION, chkResVec[3]);
				if (chkResVec[3] != null)
					atRowValus.put(
						AT_BWP_CHK_MARK, chkResVec[4]);

				boolean isSucc = super.addATRow(
					AT_BROUGTH_WP_CHK_RES, atRowValus);
				if (!isSucc)
					return false;
			}
		}

		return true;
	}

	// 获得设备的检查结果
	public Vector<ATRow> searchBWPChkResult(String userName, Time chkTimeFrom, Time chkTimeTo,
		String chkItemName)
		throws DatasweepException
	{
		Vector atRows = new Vector();
		ATHandler atHandler = getFunctions().createATHandler(
			AT_BROUGTH_WP_CHK_RES);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(
			AT_BROUGTH_WP_CHK_RES);
		if (!StringUtils.isNull(userName))
			rowFilter.forColumnNameContaining(
				AT_BWP_USER_NAME, userName);

		if (!StringUtils.isNull(chkItemName))
			rowFilter.forColumnNameContaining(
				AT_BWP_CHK_ITEM_NAME, chkItemName);

		if (chkTimeFrom != null)
		{
			chkTimeFrom = super.getDayStart(chkTimeFrom);
			rowFilter.forColumnNameGreaterThanOrEqualTo(
				AT_BWP_CHK_TIME, chkTimeFrom);
		}
		if (chkTimeTo != null)
		{
			chkTimeTo = super.getDayEnd(chkTimeTo);
			rowFilter.forColumnNameLessThan(
				AT_BWP_CHK_TIME, chkTimeTo);
		}

		atRows = atHandler.getATRowsByFilter(
			rowFilter, false);
		return atRows;
	}

	// 保存带入车间的检验结果
	public boolean saveFraglieItemChkRes(Hashtable hashTb, String plName, String wcName)
		throws Exception
	{
		for (int i = 0; i < hashTb.size(); i++)
		{
			Object resObj = hashTb.get(i);
			if (resObj instanceof Object[])
			{
				Object[] chkResVec = (Object[]) resObj;
				String chkItemName = (String) chkResVec[0];
				int chkRes = new Integer(chkResVec[1].toString()).intValue();

				Hashtable<String, Object> atRowValus = new Hashtable<String, Object>();

				atRowValus.put(
					AT_FRAGILE_PL_NAME, plName);
				atRowValus.put(
					AT_FRAGILE_WC_NAME, wcName);
				atRowValus.put(
					AT_FRAGILE_CHK_ITEM_NAME, chkItemName);
				atRowValus.put(
					AT_FRAGILE_CHK_RESULT, chkRes);
				atRowValus.put(
					AT_FRAGILE_CHK_TIME, getFunctions().createTime());
				atRowValus.put(
					AT_FRAGILE_CHK_USER, getFunctions().getCurrentUser().getName());
				if (chkResVec[2] != null)
					atRowValus.put(
						AT_FRAGILE_CHK_CORRECT_ACT, chkResVec[2]);
				if (chkResVec[3] != null)
					atRowValus.put(
						AT_FRAGILE_CHK_RES_MARK, chkResVec[3]);

				boolean isSucc = super.addATRow(
					AT_FRAGILE_CHK_RES, atRowValus);
				if (!isSucc)
					return false;
			}
		}

		return true;
	}

	// 获得设备的检查结果
	public Vector<ATRow> searchFraglieItemChkResult(String plName, String wcName, Time chkTimeFrom,Time chkTimeTo,
		String chkItemName)
		throws DatasweepException
	{
		Vector atRows = new Vector();
		ATHandler atHandler = getFunctions().createATHandler(
			AT_FRAGILE_CHK_RES);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(
			AT_FRAGILE_CHK_RES);
		if (!StringUtils.isNull(plName))
			rowFilter.forColumnNameContaining(
				AT_FRAGILE_PL_NAME, plName);

		if (!StringUtils.isNull(wcName))
			rowFilter.forColumnNameContaining(
				AT_FRAGILE_WC_NAME, wcName);

		if (!StringUtils.isNull(chkItemName))
			rowFilter.forColumnNameContaining(
				AT_FRAGILE_CHK_ITEM_NAME, chkItemName);

		if (chkTimeFrom != null)
		{
			chkTimeFrom = super.getDayStart(chkTimeFrom);
			rowFilter.forColumnNameGreaterThanOrEqualTo(
				AT_FRAGILE_CHK_TIME, chkTimeFrom);
		}
		if (chkTimeTo != null)
		{
			chkTimeTo = super.getDayEnd(chkTimeTo);
			rowFilter.forColumnNameLessThan(
				AT_FRAGILE_CHK_TIME, chkTimeTo);
		}

		atRows = atHandler.getATRowsByFilter(
			rowFilter, false);
		return atRows;

	}

	// 保存生产前检查的结果
	public boolean saveOnlineBeforeProduceChkRes(
		HashMap<Vector<String>, HashMap<String, String>> chkResult)
		throws DatasweepException
	{
		if (chkResult == null || chkResult.size() == 0)
			return false;

		Iterator<Entry<Vector<String>, HashMap<String, String>>> iter = chkResult.entrySet()
			.iterator();
		ATHandler chkHandler = getFunctions().createATHandler(
			AT_BEF_PRODUCE_CHK);
		while (iter.hasNext())
		{
			Entry<Vector<String>, HashMap<String, String>> next = iter.next();
			Vector<String> chkRes = next.getKey();
			ATRow chkATRow = chkHandler.createATRow();
			chkATRow.setName(chkRes.get(
				0).toString() + getFunctions().createTime().toLong());
			chkATRow.setValue(
				AT_BEF_PRODUCE_CHK_TIME, getFunctions().createTime());
			chkATRow.setValue(
				AT_BEF_PRODUCE_CHK_USER, getFunctions().getCurrentUser().getName());
			chkATRow.setValue(
				AT_BEF_PRODUCE_CHK_MARK, chkRes.get(1));
			chkATRow.setValue(
				AT_BEF_PRODUCE_CHK_EQU_NAME, chkRes.get(0));
			chkATRow.setValue(
				AT_BEF_PRODUCE_CHK_PL_NAME, chkRes.get(2));
			chkATRow.setValue(
				AT_BEF_PRODUCE_CHK_AREA_NAME, chkRes.get(3));
			Response response = chkATRow.save(
				getTrxTime(), null, null);
			if (response.isError())
				continue;

			if (!(chkATRow instanceof ParentATRow))
				continue;

			ParentATRow chkParentATRow = (ParentATRow) chkATRow;
			HashMap<String, String> chkItemRes = next.getValue();
			Iterator<Entry<String, String>> itemResults = chkItemRes.entrySet().iterator();
			while (itemResults.hasNext())
			{
				Entry<String, String> itemRes = itemResults.next();
				String chkItemName = itemRes.getKey();
				String chkItemContent = itemRes.getValue();
				Response res = chkParentATRow.createDependentATRow(
					getFunctions().getATDefinition(
						AT_BEF_PRODUCE_CHK_ITEM_RESULT), getTrxTime().toLong() + "");
				if (res.isError())
					continue;

				DependentATRow deATRow = (DependentATRow) res.getResult();
				deATRow.setValue(
					AT_BEF_PRODUCE_CHK_ITEM_NAME, chkItemName);
				deATRow.setValue(
					AT_BEF_PRODUCE_CHK_RESULT, chkItemContent);

				response = chkParentATRow.save(
					getTrxTime(), null, null);
				if (response.isError())
				{
					throw new DatasweepException(response);
				}
			}
		}

		return true;
	}

}
